﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FySystem.Caches
{
    public interface ICache<T>
    {
        /// <summary>
        /// 失效时间，<=0表示永不失效
        /// </summary>
        public long ExpireSeconds { get; set; }

        /// <summary>
        /// 设置获取数据规则
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fun"></param>
        public void SetGetRule(Func<string, T> fun);

        /// <summary>
        /// 设置写入数据规则（用于在写入缓存前对对象进行一定的处理）
        /// </summary>
        /// <param name="fun"></param>
        public void SetBeforeSetDataRule(Func<string, T, T> fun);

        /// <summary>
        /// 设置写入数据规则
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fun"></param>
        public void SetSetDataRule(Action<string, T> fun);

        /// <summary>
        /// 获取删除数据规则
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fun"></param>
        public void SetDeleteRule(Action<string, T> fun);

        /// <summary>
        /// 获取缓存中的数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public T this[string key] { get; }

        /// <summary>
        /// 清空缓存中的所有数据，根据传入的数据重新初始化缓存
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public bool ResetCacheData(Dictionary<string, T> datas);

        /// <summary>
        /// 向缓存中插入或者更新数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Set(string key, T value, bool updateDataSource = true);

        /// <summary>
        /// 删除缓存中的数据（同时会执行DeleteRule）
        /// </summary>
        /// <param name="key"></param>
        /// <returns>删除成功返回true，没有数据返回false</returns>
        public bool Delete(string key);

        /// <summary>
        /// 判断缓存中数据是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <param name="searchFromDataSource">如果缓存中不存在，是否还从数据源中查找</param>
        /// <returns></returns>
        public bool IsInCache(string key, bool searchFromDataSource);

        /// <summary>
        /// 强制让某个缓存过期（不会执行DeleteRule）
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public T InvalidateItem(string key);

        /// <summary>
        /// 强制让所有缓存失效（删除所有）
        /// </summary>
        /// <returns></returns>
        public bool InvalidateAll();
    }
}
